package net.sql.config;

import java.util.Vector;

import net.dotool.NumberTools;

/**
 * @author 李岩飞
 * @email eliyanfei@126.com
 * @date 2018年11月7日上午10:25:44
 */
public class SqlMonitor implements Comparable<SqlMonitor> {
	private long lastUseTime;// 最近一次用时
	private Vector<Long> timeList = new Vector<Long>();
	private long counter = 0;// 使用量
	private String sqlId;// ID
	private String sqls;// select语句

	public String getSqlId() {
		return sqlId;
	}

	public void setSqlId(String sqlId) {
		this.sqlId = sqlId;
	}

	public void setSqls(String sqls) {
		this.sqls = sqls;
	}

	public String getSqls() {
		return sqls;
	}

	public long getCounter() {
		return counter;
	}

	/**
	 * 最后耗时
	 */
	public long getLastUseTime() {
		return lastUseTime;
	}

	/**
	 * 平均耗时
	 */
	public long getAvgUseTime() {
		long total = 0;
		int c = 0;
		for (long t : timeList) {
			total += t;
			c++;
		}
		return (long) NumberTools.formatFmZero(total, c);
	}

	/**
	 * 监控
	 */
	public void monitor(long time) {
		try {
			counter++;
			lastUseTime = time;
			if (timeList.size() > 5)
				timeList.remove(0);
			timeList.add(time);
		} catch (Exception e) {
		}
	}

	@Override
	public int compareTo(SqlMonitor o) {
		return (int) (this.lastUseTime - o.lastUseTime);
	}
}
